package cn.iocoder.yudao.module.tms.controller.admin.bandinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.tms.controller.admin.bandinfo.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.bandinfo.BandOrderDO;
import cn.iocoder.yudao.module.tms.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.tms.dal.dataobject.companybaseinfo.CompanyBaseInfoDO;
import cn.iocoder.yudao.module.tms.service.bandinfo.BandOrderService;
import cn.iocoder.yudao.module.tms.service.companybaseinfo.CompanyBaseInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - Band 与订单关联")
@RestController
@RequestMapping("/tms/band-order")
public class BandOrderController {

    @Resource
    private BandOrderService bandOrderService;

    @Resource
    private CompanyBaseInfoService companyBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建 Band 与订单关联")
    @PreAuthorize("@ss.hasPermission('tms:band-order:create')")
    public CommonResult<Long> createBandOrder(@Valid @RequestBody BandOrderSaveReqVO createReqVO) {
        return success(bandOrderService.createBandOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 Band 与订单关联")
    @PreAuthorize("@ss.hasPermission('tms:band-order:update')")
    public CommonResult<Boolean> updateBandOrder(@Valid @RequestBody BandOrderSaveReqVO updateReqVO) {
        bandOrderService.updateBandOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 Band 与订单关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:band-order:delete')")
    public CommonResult<Boolean> deleteBandOrder(@RequestParam("id") Long id) {
        bandOrderService.deleteBandOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 Band 与订单关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:band-order:query')")
    public CommonResult<BandOrderRespVO> getBandOrder(@RequestParam("id") Long id) {
        BandOrderDO bandOrder = bandOrderService.getBandOrder(id);
        BandOrderRespVO respVO = BeanUtils.toBean(bandOrder, BandOrderRespVO.class);
        // 设置公司简称
        if (respVO.getCompanyId() != null) {
            CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
            if (company != null) {
                respVO.setCompanyShortName(company.getShortName());
            }
        }
        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得 Band 与订单关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('tms:band-order:query')")
    public CommonResult<List<BandOrderRespVO>> getBandOrderList(@RequestParam("ids") List<Long> ids) {
        List<BandOrderDO> list = bandOrderService.getBandOrderList(ids);
        List<BandOrderRespVO> voList = BeanUtils.toBean(list, BandOrderRespVO.class);
        // 设置公司简称
        for (BandOrderRespVO respVO : voList) {
            if (respVO.getCompanyId() != null) {
                CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
                if (company != null) {
                    respVO.setCompanyShortName(company.getShortName());
                }
            }
        }
        return success(voList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得 Band 与订单关联分页")
    @PreAuthorize("@ss.hasPermission('tms:band-order:query')")
    public CommonResult<PageResult<BandOrderRespVO>> getBandOrderPage(@Valid BandOrderPageReqVO pageReqVO) {
        PageResult<BandOrderDO> pageResult = bandOrderService.getBandOrderPage(pageReqVO);
        PageResult<BandOrderRespVO> respPageResult = BeanUtils.toBean(pageResult, BandOrderRespVO.class);
        // 设置公司简称
        for (BandOrderRespVO respVO : respPageResult.getList()) {
            if (respVO.getCompanyId() != null) {
                CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
                if (company != null) {
                    respVO.setCompanyShortName(company.getShortName());
                }
            }
        }
        return success(respPageResult);
    }

    @GetMapping("/unbound-order-page")
    @Operation(summary = "获得未绑定到指定Band的订单分页")
    @PreAuthorize("@ss.hasPermission('tms:band-order:query')")
    public CommonResult<PageResult<UnboundOrderPageRespVO>> getUnboundOrderPage(
            @Valid UnboundOrderPageReqVO pageReqVO) {
        PageResult<OrderDO> pageResult = bandOrderService.getUnboundOrderPage(pageReqVO.getBandCode(),
                pageReqVO.getOrderCode(), pageReqVO);

        PageResult<UnboundOrderPageRespVO> unboundOrderPageRespVOPageResult = new PageResult<>();
        List<UnboundOrderPageRespVO> voList = BeanUtils.toBean(pageResult.getList(), UnboundOrderPageRespVO.class);
        // 设置公司简称
        for (UnboundOrderPageRespVO respVO : voList) {
            if (respVO.getCompanyId() != null) {
                CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
                if (company != null) {
                    respVO.setCompanyShortName(company.getShortName());
                }
            }
        }
        unboundOrderPageRespVOPageResult.setList(voList);
        unboundOrderPageRespVOPageResult.setTotal(pageResult.getTotal());
        return success(unboundOrderPageRespVOPageResult);
    }

    @GetMapping("/stats")
    @Operation(summary = "查询指定Band下绑定的订单数量与累计金额")
    @Parameter(name = "bandCode", description = "Band Code", required = true, example = "CODE15239")
    @PreAuthorize("@ss.hasPermission('tms:band-order:query')")
    public CommonResult<BandOrderStatsRespVO> getBandOrderStats(@RequestParam("bandCode") String bandCode) {
        return success(bandOrderService.getBandOrderStats(bandCode));
    }

}